﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Editor.Properties;
using ResourceEditor.Core;
using Microsoft.SqlServer.MessageBox;

namespace Editor
{
    public partial class Main : Form
    {
        #region Properties
        internal static ResourceFile Current { get; private set; }
        internal static ResourceAssembly Manager { get; private set; }

        public bool ChangesMode
        {
            get
            {
                return tsChanges.Checked;
            }
        }
        #endregion

        #region Ctor
        public Main()
        {
            InitializeComponent();
        }
        #endregion

        #region Events

        #region ToolStrip and MenuBar
        private void tsOpen_Click(object sender, EventArgs e)
        {
            if (Manager != null && 
                Manager.HasChanges && 
                Program.WarningYesNo(Resources.HasChanges) == DialogResult.No)
            {
                return;
            }

            if (fdResources.ShowDialog() == DialogResult.OK)
            {
                Claer();

                try
                {
                    Manager = new ResourceAssembly(fdResources.FileName);
                }
                catch (Exception ex)
                {
                    ExceptionMessageBox mb = new ExceptionMessageBox(ex);
                    mb.Show(this);
                }
                txtFileName.Text = fdResources.FileName;
                lbResources.Items.AddRange(Manager.ResoueceNames);
            }
        }

        private void tsSaveDll_Click(object sender, EventArgs e)
        {
            if (Manager == null)
            {
                return;
            }

            if (!Manager.HasChanges)
            {
                Program.Information(Resources.NoChanges);
                return;
            }

            SaveEmbedded();
        }

        private void tsSaveResx_Click(object sender, EventArgs e)
        {
            if (Manager == null)
            {
                return;
            }

            SaveResx();
        }

        private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Manager == null)
            {
                return;
            }

            if (!Manager.HasChanges)
            {
                Program.Information(Resources.NoChanges);
            }
            else
            {
                SaveEmbedded();
            }

            SaveResx();
        }

        private void tsChanges_Click(object sender, EventArgs e)
        {
            if (Manager != null && Current != null)
            {
                editorString1.Fill(Current.Strings);
                editorIcon1.Fill(Current.Icons);
                editorFile1.Fill(Current.Files);
                editorBitmap1.Fill(Current.Bitmaps);
                editorAudio1.Fill(Current.Audio);
            }
            else
            {
                tsChanges.Checked = !tsChanges.Checked;
            }
        }

        private void tsFind_Click(object sender, EventArgs e)
        {
            string name = Current != null ? Current.Name : null;
            editorString1.Search(name);
        }
        #endregion

        #region Form
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Manager != null &&
                Manager.HasChanges &&
                Program.WarningYesNo(Resources.HasChanges) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void lbResources_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbResources.SelectedIndex != -1)
            {
                Current = Manager[lbResources.SelectedItem.ToString()];
                editorString1.Fill(Current.Strings);
                editorBitmap1.Fill(Current.Bitmaps);
                editorIcon1.Fill(Current.Icons);
                editorFile1.Fill(Current.Files);
                editorAudio1.Fill(Current.Audio);
                editorOther1.Fill(Current.Others);
            }
        }

        #endregion

        #endregion

        #region Methods
        private void Claer()
        {
            txtFileName.Text = string.Empty;
            lbResources.Items.Clear();
            editorString1.Clear();
            editorBitmap1.Clear();
            editorIcon1.Clear();
            editorFile1.Clear();
            editorAudio1.Clear();
            editorOther1.Clear();
            Manager = null;
        }

        private void SaveEmbedded()
        {
            if (Program.WarningYesNo(Resources.Override) == DialogResult.Yes)
            {
                try
                {
                    Manager.SaveEmbedded();
                    Program.Information(Resources.SavedSuccessfully);
                    Claer();
                }
                catch (Exception ex)
                {
                    ExceptionMessageBox mb = new ExceptionMessageBox(ex);
                    mb.Show(this);
                }
            }
        }

        private void SaveResx()
        {
            try
            {
                Manager.SaveResx();
                Program.Information(Resources.CreatedSuccessfully);
            }
            catch (Exception ex)
            {
                ExceptionMessageBox mb = new ExceptionMessageBox(ex);
                mb.Show(this);
            }
        }

        internal void SelectTab(ResourceType resourceType)
        {
            switch (resourceType)
            {
                case ResourceType.String:
                    tcResources.SelectedTab = tcResources.TabPages["tpStrings"];
                    break;
                case ResourceType.Bitmap:
                    tcResources.SelectedTab = tcResources.TabPages["tpBitmaps"];
                    break;
                case ResourceType.Icon:
                    tcResources.SelectedTab = tcResources.TabPages["tpIcons"];
                    break;
                case ResourceType.Audio:
                    tcResources.SelectedTab = tcResources.TabPages["tpAudio"];
                    break;
                case ResourceType.File:
                    tcResources.SelectedTab = tcResources.TabPages["tpFiles"];
                    break;
                case ResourceType.Other:
                    tcResources.SelectedTab = tcResources.TabPages["tpFiles"];
                    break;
                default:
                    throw new NotImplementedException(resourceType.ToString());
            }
        }

        internal void SelectResource(ResourceFile searchIn)
        {
            if (Current != searchIn)
            {
                lbResources.SelectedItem = searchIn.Name;
            }
        }
        #endregion        
    }
}
